package shop.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.google.gson.Gson;

import shop.dao.AddressDao;
import shop.dao.impl.AddressDaoImpl;
import shop.entity.Address;
import shop.entity.User;

@WebServlet("/AddressServlet")
public class AddressServlet extends BaseServlet {
	private AddressDao dao = new AddressDaoImpl();
	
	//查询登录用户的默认地址
	public void findDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Address address = dao.findDefault(user.getId());
		session.setAttribute("defaultAddress", address);
		response.sendRedirect("buyCarTwo.jsp");
	}
	
	//查询登录用户的所有地址
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user!=null) {
			List<Address> addressList = dao.findAll(user.getId());
			//System.out.println(addressList);
			session.setAttribute("addressList", addressList);
		}
		response.sendRedirect("memberAddress.jsp");
	}
	
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Address address = dao.findById(id);
		//对象转json
		String json = new Gson().toJson(address);
		//System.out.println(json);
		response.getWriter().print(json);
	}
	
	public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String postalCode = request.getParameter("postalCode");
		String phone = request.getParameter("phone");
		String tel = request.getParameter("tel");
		String sign = request.getParameter("sign");
		String remark = request.getParameter("remark");
		String deliverTime = request.getParameter("deliverTime");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date deliverTime1 = null;
		try {
			if(deliverTime!=null&&deliverTime!="") {
				deliverTime1 = format.parse(deliverTime);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Address addr = new Address(id, user.getId(), address, tel, phone, postalCode, email, name, sign, deliverTime1, null, 0, remark);
		dao.edit(addr);
		findAll(request, response);
	}
	
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		dao.del(id);
		findAll(request, response);
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String postalCode = request.getParameter("postalCode");
		String phone = request.getParameter("phone");
		String tel = request.getParameter("tel");
		String sign = request.getParameter("sign");
		String remark = request.getParameter("remark");
		String deliverTime = request.getParameter("deliverTime");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date deliverTime1 = null;
		try {
			if(deliverTime!=null&&deliverTime!="") {
				deliverTime1 = format.parse(deliverTime);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Address addr = new Address(user.getId(), address, tel, phone, postalCode, email, name, sign, deliverTime1, null, 0, remark);
		dao.add(addr);
		findAll(request, response);
	}
	
	public void setDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Address address = dao.findDefault(user.getId());

		//如果用户已设置过默认收货地址，先取消之前的默认收货地址
		if(address!=null) {
			dao.removeDefault(address.getId());
		}
		//把当前地址设置为默认收货地址
		dao.setDefault(id);
		findAll(request, response);
	}

}
